using System;
using System.Drawing;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pegasus.Resources;
using Pegasus.Runtime.Serialization.Formatters.Xml;

namespace Pegasus.UnitTests.Runtime.Serialization.Formatters.Xml.XmlFormatter2Tests
{
	/// <summary>
	/// This is a test class for Pegasus.Runtime.Serialization.Formatters.Xml.XmlFormatter2
	/// and will check the arrays of basic types and complex types.
	///</summary>
	[TestClass]
	public class XmlFormatter2ArrayTests
	{
		// Local Instance Values
		private TestContext m_testContextInstance;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:XmlFormatter2ArrayTests"/> class.
		/// </summary>
		public XmlFormatter2ArrayTests()
		{
		}

		/// <summary>
		/// Gets or sets the test context which provides information about and 
		/// functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return m_testContextInstance;
			}
			set
			{
				m_testContextInstance = value;
			}
		}

		/// <summary>
		/// Test the Serialize of byte arrays.
		/// </summary>
		[TestMethod]
		public void SerializeArrayByteTest()
		{
			byte interval = ( byte.MaxValue - byte.MinValue ) / XmlFormatter2Helper.TestLoops;
			byte value = byte.MinValue;

			byte[] orginalArray = new byte[ XmlFormatter2Helper.TestLoops ];
			for( long x = 0; x < XmlFormatter2Helper.TestLoops; x++ )
			{
				orginalArray[ x ] = value;
				value += interval;
			}

			byte[] finishedArray = (byte[]) XmlFormatter2Helper.SerializeDeserialize( orginalArray );

			Assert.AreEqual( finishedArray.Length, orginalArray.Length, "Array length is not correct" );

			int count = finishedArray.Length;
			for( long x = 0; x < count; x++ )
			{
				Assert.AreEqual( orginalArray[ x ], finishedArray[ x ], "Array values {0} {1} did not serialize correctly", orginalArray[ x ], finishedArray[ x ] );
			}
		}

		/// <summary>
		/// Serializes the array byte using the Icon object (it's data is stored in a byte array).
		/// </summary>
		[TestMethod]
		public void SerializeArrayByteTest2()
		{
			Icon firstIcon = XmlFormatter2Helper.EmbeddedResouces.GetIcon( "TestIcon.ico" );
			Icon secondIcon = (Icon) XmlFormatter2Helper.SerializeDeserialize( firstIcon );

			Assert.IsNotNull( firstIcon );
			Assert.IsNotNull( secondIcon );
		}

		/// <summary>
		/// Deserializes the array byte using the Icon object (it's data is stored in a byte array).
		/// </summary>
		[TestMethod]
		public void SerializeArrayByteTest3()
		{
			Icon icon = null;

			string iconXml = XmlFormatter2Helper.EmbeddedResouces.GetXml( "SerializedIcon.xml" );
			byte[] bytesXml = Encoding.UTF8.GetBytes( iconXml );

			using( MemoryStream memStream = new MemoryStream( bytesXml ) )
			{
				XmlFormatter2 xmlFormatter = new XmlFormatter2();
				icon = (Icon) xmlFormatter.Deserialize( memStream );
			}

			Assert.IsNotNull( icon );
		}

		/// <summary>
		/// Test the Serialize of int arrays.
		/// </summary>
		[TestMethod]
		public void SerializeArrayIntTest()
		{
			int interval = (int) ( ( (long) int.MaxValue - (long) int.MinValue ) / (long) XmlFormatter2Helper.TestLoops );
			int value = int.MinValue;

			int[] orginalArray = new int[ XmlFormatter2Helper.TestLoops ];
			for( long x = 0; x < XmlFormatter2Helper.TestLoops; x++ )
			{
				orginalArray[ x ] = value;
				value += interval;
			}

			int[] finishedArray = (int[]) XmlFormatter2Helper.SerializeDeserialize( orginalArray );

			Assert.AreEqual( finishedArray.Length, orginalArray.Length, "Array length is not correct" );

			int count = finishedArray.Length;
			for( long x = 0; x < count; x++ )
			{
				Assert.AreEqual( orginalArray[ x ], finishedArray[ x ], "Array values {0} {1} did not serialize correctly", orginalArray[ x ], finishedArray[ x ] );
			}
		}

		/// <summary>
		/// Test the Serialize method (basic object)
		/// </summary>
		[TestMethod]
		public void SerializeArrayObjectTest()
		{
			SerializationRoot[] orginalObjets = new SerializationRoot[ XmlFormatter2Helper.TestLoops ];
			for( long x = 0; x < XmlFormatter2Helper.TestLoops; x++ )
			{
				string strNum = x.ToString();

				// Build the object to serialize 
				SerializationRoot startObj = new SerializationRoot();
				startObj.Name = XmlFormatter2Helper.StringTestValue + strNum;
				startObj.Description = XmlFormatter2Helper.StringTestValue + strNum;

				orginalObjets[ x ] = startObj;
			}

			SerializationRoot[] newObjets = (SerializationRoot[]) XmlFormatter2Helper.SerializeDeserialize( orginalObjets );

			Assert.AreEqual( newObjets.Length, orginalObjets.Length, "Array length is not correct" );

			int count = newObjets.Length;
			for( long x = 0; x < count; x++ )
			{
				// Check test results
				string strNum = x.ToString();
				SerializationRoot finishedObj = newObjets[ x ];

				Assert.IsNotNull( finishedObj, "Deserialize array element {0} return null object", x );
				Assert.AreEqual( finishedObj.Name, XmlFormatter2Helper.StringTestValue + strNum, "Name value is not right" );
				Assert.AreEqual( finishedObj.Description, XmlFormatter2Helper.StringTestValue + strNum, "Description value is not right" );
			}
		}
	}
}
